package com.huangxt.test.node;

public class TreeNode {
    private String name;
    private int num;
    private TreeNode left;
    private TreeNode right;

    public TreeNode(String name, int num) {
        this.name = name;
        this.num = num;
    }

    /**
     * 先序遍历根左右
     */
    public void preorderTraversal(){
        System.out.print(name+num+" ");
        if(left!=null)
            left.preorderTraversal();
        if(right!=null)
            right.preorderTraversal();
    }
    /**
     * 中序遍历左根右
     */
    public void inOrderTraversal(){
        if(left!=null)
            left.inOrderTraversal();
        System.out.print(name+num+" ");
        if(right!=null)
            right.inOrderTraversal();
    }
    /**
     * 后序遍历左右根
     */
    public void postOrderTraversal(){
        if(left!=null)
            left.postOrderTraversal();
        if(right!=null)
            right.postOrderTraversal();
        System.out.print(name+num+" ");
    }

    public void put(TreeNode treeNode){
        if(this.num>treeNode.num){
            if(this.left==null)
                this.left = treeNode;
            else
                this.left.put(treeNode);
        }else{
            if(this.right==null)
                this.right = treeNode;
            else
                this.right.put(treeNode);
        }
    }
}
